<?php 
if (!isset($_SESSION)) {
  session_start();
}
include APPPATH . 'libraries/defu.php';
class excution extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       protected $configs;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('smarty3');
            $this->smarty=new CI_Smarty3();
            $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
            if(!isset($_SESSION["logintry"]))$_SESSION["logintry"]=0;
        }
        function index(){
            if(!isset($_SESSION["ADP"]["USER"])){
                $this->smarty->display("backend/00_login"); 
                return;
            }
        }
        function getAlias(){
            $code=1;
            $msg= convertUrl($_POST["string"]);
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function nothing(){}
        function login(){
            try {
                $_SESSION["logintry"]++;
                $this->load->model("backend/user_model");
                $username=$this->input->post("username");
                $password=$this->input->post("password");
                $messages=array();$result=-1;
                if($username===false || $username == ""){
                    $messages[]    =   "[Shound be non-empty] User Name shound be non-empty.";
                }
                if($password===false || trim($password) == ""){
                    $messages[]    =   "[Shound be non-empty] Password shound be non-empty.";
                }
                if(count($messages)>0){
                    $result   =   -1;
                    $message    =   implode("<br/>", $messages);
                    goto result;
                }
                if($_SESSION["logintry"]>3){
                    if(!isset($_SESSION["logintrytime"]))$_SESSION["logintrytime"]=time();
                    $timedelay=60*15-(time("now")-$_SESSION["logintrytime"]);
                    if( $timedelay<=0){
                        $_SESSION["logintry"]=0;
                        unset($_SESSION["logintrytime"]);
                    }else{
                        $p=floor($timedelay/60);
                        $p=$p>0?"$p minute ":"";
                        $s=$timedelay%60;
                        $result     =   -999;
                        $message    =   "[Access Denied] Sorry, there have been more than 5 failed login<br/>attempts for this account.<br/>
                            It is temporarily blocked in 15 minutes.<br/>
                            Remaining time : $p$s second.
                            ";
                        goto result;
                    }
                }
                $rLogin=$this->user_model->checkLogin($username,$password);
                if($rLogin===true){
                    $_SESSION["logintry"]=0;
                    $result     =   1;
                    $message    =   "[Success] Login success.";
                }else{
                    $result     =   -777;
                    $message    =   "[Fail] Login fail. Please check your User Name and Password";
                }
                result:                
                echo json_encode(array("result"=>$result,"message"=>$message));
            } catch (Exception $exc) {
                echo json_encode(array("result"=>-888,"message"=>$exc->getTraceAsString()));
                //echo $exc->getTraceAsString();
            }
        }
        function logout(){
            session_destroy();
            JO_location();
        }
        function mylocation(){
            JO_location();
            
        }
        function script(){
            header("Content-type:text/javascript");
            $this->smarty->display("backend/00_subs/00_script");
        }
}
?>
